from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import Area
from .serializers import AreaSerializer,SubsAreaSeriazlier
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin,RetrieveCacheResponseMixin,CacheResponseMixin

# Create your views here.
# ListCacheResponseMixin,       列表视图
# RetrieveCacheResponseMixin,    详情视图
# CacheResponseMixin               列表+详情视图

class AreaView(CacheResponseMixin,ReadOnlyModelViewSet):
    """

    """
    #queryset = 返回查询结果集
    # queryset = Area.objects.all()
    def get_queryset(self):
        if self.action == 'list':
            #list 是省份信息
            return Area.objects.filter(parent=None)
        else:
            #retrieve 是 市 区县信息
            return Area.objects.all()

    def get_serializer_class(self):

        if self.action == 'list':
            return AreaSerializer
        else:
            return SubsAreaSeriazlier